package com.util;

import com.entity.Answer;


import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.DigestUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class Util {


    /*
        MD5加密
     */
    public static String getMd5(String pwd) {
        String md5 = DigestUtils.md5DigestAsHex(pwd.getBytes());
        return md5;
    }

    /*
        上传文件
     */
    public static Object uploadFiles(List<MultipartFile> files, String path) {
        Answer answer = new Answer();
        for (MultipartFile file : files) {
            //判断文件是否为空
            if (file.isEmpty()) {
                answer.setCode(0);
                answer.setError("文件未空！");
                return answer;
            } else {
                String fileName = file.getOriginalFilename();
                System.out.println(fileName);
                // String suffixName = fileName.substring(fileName.lastIndexOf("."));
                long size = file.getSize();
                File dest = new File(path + fileName);
                System.out.println(dest.getParentFile());
                if (!dest.getParentFile().exists()) {
                    System.out.println(dest.getParentFile());
                    //创建目录
                    dest.getParentFile().mkdirs();
                }
                try {
                    file.transferTo(dest);
                } catch (IOException e) {
                    e.printStackTrace();
                    answer.setCode(0);
                    answer.setError("文件上传失败！");
                    return answer;
                }
            }
        }
        return null;
    }

    public static String getPath() throws FileNotFoundException {
        File path = new File(ResourceUtils.getURL("classpath:").getPath());
        File upload = new File(path.getAbsolutePath(), "static/upload/");
        return upload.getAbsolutePath();
    }

       /*
       /**
     * Excel文件下载
     */

    public static Object fileDownload(HttpServletResponse response, HttpServletRequest request,Integer patientID,String name) {
        Answer answer = new Answer();
        String path = null;
        String fileName = null;
        if (patientID == null || (patientID.toString()).equals("")){
            path = "excel/MakeModel.xlsx";
        }else {
            path =  Util.getLocalPath()+Util.getExcelPath()+ patientID+"/"+name+".xlsx";
        }
        Resource resource = new ClassPathResource(path);
        //文件名
       resource.getFilename();
        try {
            File file = null;
            if (patientID == null || (patientID.toString()).equals("")){
                 file = resource.getFile();
                fileName = name + ".xlsx";
            }else {
                file = new File(path);
                fileName = file.getName();
            }

            if (file == null) {
                answer.setError("未找到文件");
                answer.setCode(0);
                return answer;
            }

            InputStream inputStream = new FileInputStream(file);

            OutputStream out = response.getOutputStream();
//            response.setContentType("application/xls");
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
            response.addHeader("Content-Type", "application/x-download");
        //    response.addHeader("Content-Disposition", "attachment; filename=\"" + new String(fileName.getBytes("UTF-8"), "ISO-8859-1") + "\".xlsx;filename*=UTF-8''" + new String(fileName.getBytes("UTF-8"), "ISO-8859-1"));
            int b = 0;
            byte[] buffer = new byte[1000000];
            while (b != -1) {
                b = inputStream.read(buffer);
                if (b != -1) out.write(buffer, 0, b);
            }
            inputStream.close();
            out.close();
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
            answer.setCode(0);
            answer.setError("未找到文件");
            return answer;
        }
        return null;
    }

    public static String getDate() {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(new Date());
    }


    public static String getLocalPath(){
        return  System.getProperty("user.dir") + "/static/";
    }

    public static String getExcelPath(){
        return "/patient/excel/";
    }



}
